package com.hanbang.maige.view.quanzi.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import com.hanbang.maige.R;
import com.hanbang.maige.code.base.baseadapter.ViewHolder;
import com.hanbang.maige.code.base.baseadapter.recyclerview.CommonAdapter;
import com.hanbang.maige.code.base.baseadapter.recyclerview.click.OnItemClickListener;
import com.hanbang.maige.code.base.view.activity.BaseActivity;
import com.hanbang.maige.mode.enumeration.ShowQuanXian;
import com.hanbang.maige.mode.javabean.quanzi.QuanZiQuanXianData;
import com.hanbang.maige.utils.ui.divider.HorizontalDividerItemDecoration;
import com.hanbang.maige.widget.AnimCheckBox;

import java.util.ArrayList;

import butterknife.BindView;

/**
 * 作者　　: 李坤
 * 创建时间:2016/10/31　18:19
 * 邮箱　　：496546144@qq.com
 * <p>
 * 功能介绍：发布圈子的权限
 */

public class QuanZiQuanXianActivity extends BaseActivity
        implements Toolbar.OnMenuItemClickListener, OnItemClickListener<QuanZiQuanXianData> {

    @BindView(R.id.recycleView)
    RecyclerView recycleView;

    private ArrayList<QuanZiQuanXianData> listDatas;

    CommonAdapter adapter = new CommonAdapter<QuanZiQuanXianData>(this, R.layout.item_quanzi_quanxian,
            listDatas = ShowQuanXian.getListData()
    ) {
        @Override
        public void convert(ViewHolder holder, QuanZiQuanXianData o) {
            holder.setText(R.id.title, o.getTitle());
            holder.setText(R.id.detaile, o.getDetaile());
            AnimCheckBox animCheckBox = holder.getView(R.id.animCheckBox);
            animCheckBox.setChecked(o.isSelect());
            animCheckBox.setAutoSelect(false);
        }

    };

    public static void startUI(BaseActivity context) {
        Intent intent = new Intent(context, QuanZiQuanXianActivity.class);
        context.startActivityForResult(intent, context.REQUEST_CODE + 1);
    }

    @Override
    public int getContentView() {
        return R.layout.activity_quanzi_quanxian;
    }

    @Override
    public void initView() {
        toolbar.setBack(this);
        toolbar.setSuperTitle("谁可以看");
        toolbar.addAction(0, "完成");
        toolbar.setOnMenuItemClickListener(this);
        recycleView.setLayoutManager(new LinearLayoutManager(this));
        recycleView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(this)
                .sizeResId(R.dimen.dp_1).colorResId(R.color.gray_ee)
                .build());
        recycleView.setAdapter(adapter);

        adapter.setOnItemClickListener(this);
    }

    @Override
    public void parseIntent(Intent intent) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public boolean onMenuItemClick(MenuItem item) {


        for (QuanZiQuanXianData d : listDatas) {
            if (d.isSelect()) {
                Intent intent = new Intent();
                intent.putExtra("data", d);
                setResult(RESULT_OK, intent);
                finish();
                return true;
            }
        }


        return true;
    }

    @Override
    public void onItemClick(ViewGroup parent, View view, QuanZiQuanXianData data, int position) {
        for (QuanZiQuanXianData d : listDatas) {
            d.setSelect(d == data);
        }
        adapter.notifyDataSetChanged();
    }
}
